home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / KDChartDataIntern.h < prev    next >
Encoding:
C/C++ Source or Header  |  2007-05-30  |  13.0 KB  |  433 lines

  1. /* -*- Mode: C++ -*-
  2.    KDChart - a multi-platform charting engine
  3. */
  4.  
  5. /****************************************************************************
  6.  ** Copyright (C) 2001-2003 Klar∩┐╜vdalens Datakonsult AB.  All rights reserved.
  7.  **
  8.  ** This file is part of the KDChart library.
  9.  **
  10.  ** This file may be distributed and/or modified under the terms of the
  11.  ** GNU General Public License version 2 as published by the Free Software
  12.  ** Foundation and appearing in the file LICENSE.GPL included in the
  13.  ** packaging of this file.
  14.  **
  15.  ** Licensees holding valid commercial KDChart licenses may use this file in
  16.  ** accordance with the KDChart Commercial License Agreement provided with
  17.  ** the Software.
  18.  **
  19.  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  20.  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  **
  22.  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
  23.  **   information about KDChart Commercial License Agreements.
  24.  **
  25.  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
  26.  ** licensing are not clear to you.
  27.  **
  28.  **********************************************************************/
  29. #ifndef __KDCHARTDATAINTERN_H__
  30. #define __KDCHARTDATAINTERN_H__
  31.  
  32. #include <qvariant.h>
  33. #include <qstring.h>
  34. #include <qdatetime.h>
  35. #include "KDChartGlobal.h"
  36.  
  37. /**
  38.    \file KDChartData.h
  39.  
  40.    \brief Provides a class to encapsulate one data value in a chart.
  41. */
  42.  
  43.  
  44.  
  45. // Please leave all methods in this class inline!
  46. // It's necessary since it's part of the interface provided by KDChart
  47. class KDCHART_EXPORT KDChartData
  48. {
  49. public:
  50.     // 0. default c'tor: initializing all values as undefined
  51.  
  52.     KDChartData() :
  53.         _valueType(  QVariant::Invalid ),
  54.         _valueType2( QVariant::Invalid ),
  55.         _propSetID(  0 )
  56.     {}
  57.  
  58.     // 1. simple c'tors: used for 1-coordinate data
  59.  
  60.     KDChartData( double value ) :
  61.         _valueType(  QVariant::Double ), dValue( value ),
  62.         _valueType2( QVariant::Invalid ),
  63.         _propSetID(  0 )
  64.     {}
  65.     /* string values are only supported for legend texts or axis labels */
  66.     KDChartData( const QString& value ) :
  67.         _valueType(  QVariant::String ), sValue( value ),
  68.         _valueType2( QVariant::Invalid ),
  69.         _propSetID(  0 )
  70.     {}
  71.     /* date/time values for /ordinate/ axes are not implemented yet
  72.        KDChartData( QDateTime value ) :
  73.        _valueType(  QVariant::DateTime ), dtValue( value ),
  74.        _valueType2( QVariant::Invalid ),
  75.        _propSetID(  0 )
  76.        {}*/
  77.  
  78.     // 2. complex c'tors: used for 2-coordinate data
  79.  
  80.     // 2.a) with additional Date/Time: normally used when Date on x-axis
  81.     //      e.g. for time related index numbers like water level measurements
  82.     KDChartData( double yValue, QDateTime xValue ) :
  83.         _valueType(  QVariant::Double   ), dValue(   yValue  ),
  84.         _valueType2( QVariant::DateTime ), dtValue2( xValue ),
  85.         _propSetID(  0 )
  86.     {}
  87.  
  88.  
  89.     KDChartData( const QVariant& _value1,
  90.                  const QVariant& _value2 )
  91.     {
  92.         switch( _value1.type() ){
  93.             case QVariant::Int:
  94.             case QVariant::UInt:
  95.             case QVariant::Double:
  96.                 _valueType = QVariant::Double;
  97.                 dValue     = _value1.toDouble();
  98.                 break;
  99.             case QVariant::DateTime:
  100.                 _valueType = QVariant::DateTime;
  101.                 dtValue    = _value1.toDateTime();
  102.                 break;
  103.             case QVariant::String:
  104.                 _valueType = QVariant::String;
  105.                 sValue     = _value1.toString();
  106.                 break;
  107.             default:
  108.                 _valueType = QVariant::Invalid;
  109.         }
  110.         switch( _value2.type() ){
  111.             case QVariant::Int:
  112.             case QVariant::UInt:
  113.             case QVariant::Double:
  114.                 _valueType2 = QVariant::Double;
  115.                 dValue2     = _value2.toDouble();
  116.                 break;
  117.             case QVariant::DateTime:
  118.                 _valueType2 = QVariant::DateTime;
  119.                 dtValue2    = _value2.toDateTime();
  120.                 break;
  121.             default:
  122.                 _valueType2 = QVariant::Invalid;
  123.         }
  124.         _propSetID = 0;
  125.     }
  126.  
  127.     /* date/time values for /ordinate/ axes are not implemented yet
  128.        KDChartData( QDateTime yValue, QDateTime xValue ) :
  129.        _valueType(  DateTime ), dtValue(  yValue  ),
  130.        _valueType2( DateTime ), dtValue2( xValue ),
  131.        _propSetID(  0 )
  132.        {}*/
  133.     // 2.b) with additional Double: may be used for mathematical data...
  134.     KDChartData( double yValue, double xValue ) :
  135.         _valueType(  QVariant::Double ), dValue(  yValue  ),
  136.         _valueType2( QVariant::Double ), dValue2( xValue ),
  137.         _propSetID(  0 )
  138.     {}
  139.     /* date/time values for /ordinate/ axes are not implemented yet
  140.        KDChartData( QDateTime yValue, double xValue ) :
  141.        _valueType(  DateTime ), dtValue( yValue  ),
  142.        _valueType2( Double   ), dValue2( xValue ),
  143.        _propSetID(  0 )
  144.        {}*/
  145.  
  146. /*
  147.     /-**
  148.        Copy constructor.
  149.  
  150.        \sa setData
  151.     * /
  152.     KDChartData( const KDChartData& other ) :
  153.     {
  154.         setData( other );
  155.     }
  156.  
  157.     /-**
  158.        Assignment operator.
  159.  
  160.        \sa setData
  161.     * /
  162.     KDChartData& operator=( const KDChartData& R )
  163.     {
  164.         setData( R );
  165.         return *this;
  166.     }
  167. */
  168.     /**
  169.        Compare operator.
  170.  
  171.        \sa isEqual
  172.     */
  173.     bool operator==( const KDChartData& it ) const
  174.     {
  175.         return isEqual(*this, it);
  176.     }
  177.  
  178.     /**
  179.        Compare operator.
  180.  
  181.        \sa isEqual
  182.     */
  183.     bool operator!=( const KDChartData& it ) const
  184.     {
  185.         return !isEqual(*this, it);
  186.     }
  187.  
  188.     /**
  189.        Compare method.
  190.  
  191.        \note The property set ID is <b>not</b> taken into account while comparing.
  192.        Two KDChartData are considered equal if their data values are equal - the
  193.        property set ID is ignored.
  194.     */
  195.     bool isEqual( const KDChartData& a, const KDChartData& b ) const
  196.     {
  197.         bool bRet = (a.hasValue( 1 ) == b.hasValue( 1 )) &&
  198.                     (a.hasValue( 2 ) == b.hasValue( 2 ));
  199.         if ( bRet && a.hasValue( 1 ) ) {
  200.             bRet = a.valueType( 1 ) == b.valueType( 1 );
  201.             if ( bRet ) {
  202.                 switch ( a.valueType( 1 ) ) {
  203.                 case QVariant::String:
  204.                     bRet = a.stringValue( 1 ) == b.stringValue( 1 );
  205.                     break;
  206.                 case QVariant::Double:
  207.                     bRet = a.doubleValue( 1 ) == b.doubleValue( 1 );
  208.                     break;
  209.                 case QVariant::DateTime:
  210.                     bRet = a.dateTimeValue( 1 ) == b.dateTimeValue( 1 );
  211.                     break;
  212.                 default:
  213.                     bRet = false;
  214.                 }
  215.             }
  216.             if ( bRet && a.hasValue( 2 ) ) {
  217.                 bRet = a.valueType( 2 ) == b.valueType( 2 );
  218.                 if ( bRet ) {
  219.                     switch ( a.valueType( 2 ) ) {
  220.                         // note: the 2nd value can not be a string
  221.                         //       - must be a date or a number!
  222.                     case QVariant::Double:
  223.                         bRet = a.doubleValue( 2 ) == b.doubleValue( 2 );
  224.                         break;
  225.                     case QVariant::DateTime:
  226.                         bRet = a.dateTimeValue( 2 ) == b.dateTimeValue( 2 );
  227.                         break;
  228.                     default:
  229.                         bRet = false;
  230.                     }
  231.                 }
  232.             }
  233.             // Note: We do *not* compare the _propSetID here since it contains
  234.             //       no values but is used to handle some layout information...
  235.         }
  236.         return bRet;
  237.     }
  238.  
  239.     /**
  240.        Method setAll.
  241.  
  242.        \note The property set ID is <b>also</b> changed by the setAll method.
  243.  
  244.        \sa setData
  245.     */
  246.     void setAll( const KDChartData& R )
  247.     {
  248.         setData( R );
  249.         setPropertySet( R.propertySet() );
  250.     }
  251.  
  252.  
  253.     /**
  254.        Method setData.
  255.  
  256.        \note The property set ID is <b>not</b> changed by the setData method.
  257.        If you want to set it please call \c KDChartData::setPropertySet() explicitely.
  258.  
  259.        \sa setAll
  260.     */
  261.     void setData( const KDChartData& R )
  262.     {
  263.         if( &R != this ){
  264.             _valueType  = R._valueType;
  265.             _valueType2 = R._valueType2;
  266.             switch ( valueType( 1 ) ) {
  267.             case QVariant::String:
  268.                 sValue  = R.sValue;
  269.                 break;
  270.             case QVariant::Double:
  271.                 dValue  = R.dValue;
  272.                 break;
  273.             case QVariant::DateTime:
  274.                 dtValue = R.dtValue;
  275.                 break;
  276.             default:
  277.                 /* NOOP */;
  278.             }
  279.             switch ( valueType( 2 ) ) {
  280.                 // note: the 2nd value can not be a string
  281.                 //       - must be a date or a number!
  282.             case QVariant::Double:
  283.                 dValue2  = R.dValue2;
  284.                 break;
  285.             case QVariant::DateTime:
  286.                 dtValue2 = R.dtValue2;
  287.                 break;
  288.             default:
  289.                 /* NOOP */;
  290.             }
  291.             // Note: We do *not* copy the _propSetID here since it contains
  292.             //       no values but is used to handle some layout information...
  293.         }
  294.     }
  295.  
  296.     QVariant::Type valueType( int valNo=1 ) const
  297.     {
  298.         return (1 == valNo)
  299.                     ? _valueType
  300.                     : _valueType2;
  301.     }
  302.     bool hasValue( int valNo=1 ) const
  303.     {
  304.         return (1 == valNo)
  305.             ?  (_valueType != QVariant::Invalid)
  306.             : ((_valueType2 == QVariant::Double) || (_valueType2 == QVariant::DateTime));
  307.     }
  308.     /* string values are only supported for legend texts or axis labels */
  309.     bool isString( int valNo=1 ) const
  310.     {
  311.         return (1 == valNo)
  312.                     ? (_valueType == QVariant::String)
  313.                     : false;
  314.     }
  315.     bool isDouble( int valNo=1 ) const
  316.     {
  317.         return (1 == valNo)
  318.                     ? (_valueType == QVariant::Double)
  319.                     : (_valueType2 == QVariant::Double);
  320.     }
  321.     bool isDateTime( int valNo=1 ) const
  322.     {
  323.         return (1 == valNo)
  324.                     ? (_valueType == QVariant::DateTime)
  325.                     : (_valueType2 == QVariant::DateTime);
  326.     }
  327.  
  328.  
  329.     void clearValue()
  330.     {
  331.         _valueType  = QVariant::Invalid;
  332.         _valueType2 = QVariant::Invalid;
  333.         _propSetID  = 0;
  334.     }
  335.  
  336.     QVariant value( int valNo=1 ) const
  337.     {
  338.         if( 1 == valNo )
  339.             switch ( valueType( 1 ) ) {
  340.             case QVariant::String:
  341.                 return sValue;
  342.             case QVariant::Double:
  343.                 return dValue;
  344.             case QVariant::DateTime:
  345.                 return dtValue;
  346.             default:
  347.                 return QVariant();
  348.             }
  349.         else if( 2 == valNo )
  350.             switch ( valueType( 2 ) ) {
  351.             case QVariant::Double:
  352.                 return dValue2;
  353.             case QVariant::DateTime:
  354.                 return dtValue2;
  355.             default:
  356.                 return QVariant();
  357.             }
  358.         else
  359.             return QVariant();
  360.     }
  361.  
  362.     /* string values are only supported for legend texts or axis labels */
  363.     QString stringValue( int valNo=1 ) const
  364.     {
  365.         // note: the 2nd value can not be a string
  366.         //       - must be a date or a number!
  367.         if ((1 == valNo) && isString( valNo ))
  368.             return sValue;
  369.         else
  370.             return QString::null;
  371.     }
  372.     double doubleValue( int valNo=1 ) const
  373.     {
  374.         return isDouble( valNo )
  375.             ? ((1 == valNo) ? dValue : dValue2)
  376.             : DBL_MIN;
  377.     }
  378.     QDateTime dateTimeValue( int valNo=1 ) const
  379.     {
  380.         return isDateTime( valNo )
  381.             ? ((1 == valNo) ? dtValue : dtValue2)
  382.             : QDateTime();
  383.     }
  384.  
  385.     /**
  386.        Assign a property set to a data cell.
  387.  
  388.        \param propSetID The ID of the property set to be assigned to this data cell.
  389.        This ID can either be one of the built-in IDs documented
  390.        at KDChartPropertySet::BuiltinDataPropertySetIDs or
  391.        a special ID that was given back by a
  392.        KDChartParams::registerProperties function call.
  393.  
  394.        \sa propertySet
  395.        \sa KDCHART_PROPSET_NORMAL_DATA, KDCHART_PROPSET_TRANSPARENT_DATA
  396.     */
  397.     void setPropertySet( int propSetID = 0 )
  398.     {
  399.         _propSetID = propSetID;
  400.     }
  401.     /**
  402.        Return the ID of the property set that is assigned to this data cell.
  403.  
  404.        Use KDChartParams::properties( int ID ) for accessing the respective property set information.
  405.  
  406.        \sa setPropertySet
  407.        \sa KDCHART_PROPSET_NORMAL_DATA, KDCHART_PROPSET_TRANSPARENT_DATA
  408.     */
  409.     int propertySet() const
  410.     {
  411.         return _propSetID;
  412.     }
  413.  
  414.  
  415. private:
  416.     // OBLIGATORY 1st value: usually used for ordinate axis
  417.     QVariant::Type _valueType;
  418.     QDateTime dtValue;
  419.     double dValue;  // Sorry, d(t)Value and sValue cannot be a union,
  420.     QString sValue; // since QString has a non-default constructor.
  421.  
  422.     // OPTIONAL 2nd value: if valid, normally used for abscissa axis
  423.     // note: this 2nd value can not be a string - must be a date or a number!
  424.     QVariant::Type _valueType2;
  425.     QDateTime dtValue2;
  426.     double dValue2;
  427.  
  428.     // ID number of the property set assigned to this cell
  429.     int _propSetID;
  430. };
  431.  
  432. #endif
  433.